<?php

namespace App\Http\Controllers\admin\defaults;

use App\Helpers\commonFuc;
use App\Models\Admin;

use Carbon\Carbon;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class LoginController extends Controller
{
    private $tpview='admin.defaults.login.';
    private $thisModel;
    private $thisRequest;

    function __construct(Request $thisRequest)
    {
        $this->thisModel = DB::table('admin');
        $this->thisRequest = $thisRequest;
    }



    function index(){


        session()->put('admin', '');
        $isvercode =(int)session('isvercode');
        return view($this->tpview.'login',['isvercode'=>$isvercode]);
    }

    /**admin 123456
     * @return \App\Helpers\Json|\App\Helpers\unknown|array
     */
    function login(){


        $username = $this->thisRequest->get('username');
        $password = $this->thisRequest->get('password');
        $vercode = $this->thisRequest->get('vercode');
        $client = $this->thisRequest->get('client');

        $isvercode = session('isvercode');

      /*  if($isvercode>1){
            $captcha = \Cache::get($client);
            if (strtolower($vercode) != strtolower($captcha)){
                return commonFuc::data_reture_json('',400,'验证码不正确，请重新输入');
            }
        }*/


        $password = md5($password . 'Td');
        //$this->thisModel->where('username', $username)->update(['password'=>$password]);
       $userdata = $this->thisModel
           ->where('username', $username)
           ->where('password', $password)
           ->get()->toArray();

        if(empty($userdata)){
            session()->put('isvercode', $isvercode+1);

            return commonFuc::data_reture_json(session('isvercode'),400,'账号密码不匹配，请重新输入');
        }
        session()->put('isvercode',0);
        $userdata = $userdata[0];
        if($userdata->status!=1){
            return commonFuc::data_reture_json('',400,'该账号异常，请联系管理员');
        }
        session()->put('admin', $userdata);
        return commonFuc::data_reture_json('',200,'登陆成功');
    }
    function logout(){
        session()->put('admin', '');
        return commonFuc::data_reture_json('',200,'退出成功');
    }


    //验证码
    public function imgCode()
    {

        $phrase = new PhraseBuilder;
        // 设置验证码位数
        $code = $phrase->build(4);
        // 生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder($code, $phrase);
        // 设置背景颜色25,25,112
        $builder->setBackgroundColor(34, 0, 45);
        // 设置倾斜角度
        $builder->setMaxAngle(25);
        // 设置验证码后面最大行数
        $builder->setMaxBehindLines(5);
        // 设置验证码前面最大行数
        $builder->setMaxFrontLines(10);
        // 设置验证码颜色
        $builder->setTextColor(230, 81, 150);
        // 可以设置图片宽高及字体
        $builder->build($width = 150, $height = 40, $font = null);
        // 获取验证码的内容
        $phrase = $builder->getPhrase();

        // 把内容存入 cache，10分钟后过期
        $client_id = md5( rand(1,1000).time());
        \Cache::put($client_id, $phrase, Carbon::now()->addMinutes(10));

        // 组装接口数据
        $data = [
            'client' => $client_id,
            'captcha' => $builder->inline(),
        ];
        return $data;
    }
}
